package com.sayyi.demo.bean.copy.orika;

import com.sayyi.demo.bean.ParentDemo;
import com.sayyi.demo.bean.domain.Dest;
import com.sayyi.demo.bean.domain.DestWrapper;
import com.sayyi.demo.bean.domain.Source;
import com.sayyi.demo.bean.domain.SourceWrapper;
import ma.glasnost.orika.MapperFacade;
import ma.glasnost.orika.impl.DefaultMapperFactory;

/**
 * @author xuchuang
 * date 2021/12/20 11:11 AM
 */
public class OrikaDemo {

    public static final DefaultMapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();

    public static void main(String[] args) {
        // 这个过程创建了一个 ClassMapBuilder 实例，进行特定类的映射配置，然后将其注册到 MapperFactory 中
        // 这里可以配置映射关系、转换器等
        mapperFactory.classMap(Source.class, Dest.class)
                // 配置自定义的自动映射关系
                .field("name", "name")
                // 其余字段使用默认映射关系
                .byDefault()
                // 将映射信息注册到 MapperFactory 中。
                .register();



        mapperFactory.classMap(SourceWrapper.class, DestWrapper.class)
                .field("wrapperName", "name")
                .field("sources", "dests")
                .byDefault()
                .register();


        // 获取转换组件。线程安全组件
        final MapperFacade mapperFacade = mapperFactory.getMapperFacade();

        Source source = ParentDemo.genSource();
        final Dest dest = mapperFacade.map(source, Dest.class);
        System.out.println(dest);

        final SourceWrapper sourceWrapper = ParentDemo.genSourceWrapper();
        System.out.println(mapperFacade.map(sourceWrapper, DestWrapper.class));
    }
}
